//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;

using CommunityServer.Components;

namespace CommunityServer.Controls {

    public class ResourceControl : TaggedControl
    {
        /// <summary>
        /// Gets or sets the name of the resource to dispaly.
        /// </summary>
        [
        Bindable(true),
        Category( "Appearance" ),
        Description( "Gets or sets the name of the resource to display." ),
        DefaultValue( "" ),
        ]
        public virtual String ResourceName 
        {
            get 
            {
                Object state = ViewState["ResourceName"];
                if ( state != null ) 
                {
                    return (String)state;
                }
                return "";
            }
            set 
            {
                ViewState["ResourceName"] = value;
            }
        }

        /// <summary>
		/// Gets or sets the name of the resource file to pull from
		/// </summary>
        [
        Bindable(true),
        Category( "Appearance" ),
        Description( "Gets or sets the name of the resource file." ),
        DefaultValue( null ),
        ]
        public virtual String ResourceFile 
        {
            get 
            {
                Object state = ViewState["ResourceFile"];
                if ( state != null ) 
                {
                    return (String)state;
                }
                return "";
            }
            set 
            {
                ViewState["ResourceFile"] = value;
            }
        }

        protected override string GetText
        {
            get
            {
                if(!Globals.IsNullorEmpty(ResourceName))
                return ResourceManager.GetString( ResourceName, ResourceFile );

                return null;
            }
        }



    }

    public class TaggedLiteral : TaggedControl, IText
    {
        protected override string GetText
        {
            get
            {
                return Text;
            }
        }

        public Control Control
        {
            get{ return this;}
        }

        [
        Bindable(true),
        Category( "Appearance" ),
        Description( "Gets or sets the name of the resource to display." ),
        DefaultValue( "" ),
        ]
        public virtual String Text 
        {
            get 
            {
                return ViewState["Text"] as string;
            }
            set 
            {
                ViewState["Text"] = value;
            }
        }

    }
	
	/// <summary>
	/// A label which displays resource text by name.
	/// </summary>
	public abstract class TaggedControl : Control {
		
		protected override ControlCollection CreateControlCollection() {
			return new EmptyControlCollection( this );
		}



        protected abstract string GetText { get;}


        [
        Bindable(true),
        Category( "Appearance" ),
        Description( "Gets or sets the Tag of the resource to display." ),
        DefaultValue( HtmlTextWriterTag.Unknown ),
        ]
        public virtual HtmlTextWriterTag Tag 
        {
            get 
            {
                Object state = ViewState["Tag"];
                if ( state != null ) 
                {
                    return (HtmlTextWriterTag)state;
                }
                return HtmlTextWriterTag.Unknown;
            }
            set 
            {
                ViewState["Tag"] = value;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            
            string text = GetText;
            if(text != null)
            {
                if(Tag != HtmlTextWriterTag.Unknown)
                    writer.RenderBeginTag(Tag);

                writer.Write( text );

                if(Tag != HtmlTextWriterTag.Unknown)
                    writer.RenderEndTag();
            }

        }

		/// <summary>
		/// Not Used. Sometimes when copying ResourceControls, the desinger adds a name attribute, which causes a runtime error.
		/// </summary>
		public string Name;

	}
}
